﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Windows.Forms;
using HomeCare.View;
using System.Data;

namespace HomeCare.Model
{
   public class PerfilDados
    {
       
       Conexao con = new Conexao();

        public bool InserePerfil(Perfil perfil)
        {
       
            SqlConnection sql = con.Conectar();
            sql.Open();
            string query = String.Format("insert into Perfis (Perfil, Descricao) values ('{0}','{1}')", perfil.perfil, perfil.descricao);
            try
            {
                SqlCommand cmd = new SqlCommand(query, sql);
                cmd.ExecuteNonQuery();
                cmd.Dispose();
                sql.Close();
                return true;
            }
            catch (Exception ex)
            {
                
                //throw new Exception("Erro ao Inserir Perfil.");
                sql.Close();
                MessageBox.Show(ex.Message);
                return false;
            }
        }


        public bool AlteraPerfil(Perfil perfil)
        {
            SqlConnection sql = con.Conectar();
            sql.Open();
            string query = String.Format("update Perfis set Perfil='{0}', Descricao='{1}' where id='{2}'", perfil.perfil, perfil.descricao, perfil.id);
            try
            {
                SqlCommand cmd = new SqlCommand(query, sql);
                cmd.ExecuteNonQuery();
                cmd.Dispose();
                sql.Close();
                return true;
            }
            catch (Exception ex)
            {
                //throw new Exception("Erro ao Alterar Perfil.");
                sql.Close();
                MessageBox.Show(ex.Message);
                return false;
            }
        }


        public bool ExcluiPerfil(Perfil perfil)
        {
            SqlConnection sql = con.Conectar();
            sql.Open();
            string query = String.Format("delete from Perfis where Id='{0}'", perfil.id);
            try
            {
                SqlCommand cmd = new SqlCommand(query, sql);
                cmd.ExecuteNonQuery();
                cmd.Dispose();
                sql.Close();
                return true;
            }
            catch (Exception ex)
            {
                //throw new Exception("Erro ao Alterar Perfil.");
                sql.Close();
                MessageBox.Show(ex.Message);
                return false;
            }
        }

        public DataSet ConsultaPerfil(Perfil perfil)
        {
            SqlConnection sql = con.Conectar();
            sql.Open();
            string query = "select * from Perfis where (1=1) ";
            query += String.IsNullOrEmpty(perfil.descricao)? String.Empty :( String.Format(" and Descricao like '%{0}%' ", perfil.descricao));
            query += String.IsNullOrEmpty(perfil.perfil) ? String.Empty: (String.Format(" and Perfil like '%{0}%' ", perfil.perfil));
            query += " order by Perfil ";
            try
            {
                SqlDataAdapter dataAdapter = new SqlDataAdapter(query, sql);
                DataSet dataSet = new DataSet();
                dataAdapter.Fill(dataSet);
                sql.Close();
                return dataSet;
                
            }
            catch (Exception ex)
            {
                sql.Close();
                throw new Exception("Erro ao Consultar Perfil.");
                //MessageBox.Show(ex.Message);
                
            }
        }
       public DataSet RetornaPerfilFuncionario ()
        {
            SqlConnection sql = con.Conectar();
            sql.Open();
            string query = "select * from Perfis where id <> '0' ";
            query += " order by Perfil ";
            try
            {
                SqlDataAdapter dataAdapter = new SqlDataAdapter(query, sql);
                DataSet dataSet = new DataSet();
                dataAdapter.Fill(dataSet);
                sql.Close();
                return dataSet;

            }
            catch (Exception ex)
            {
                sql.Close();
                throw new Exception("Erro ao Consultar Perfil.");
                //MessageBox.Show(ex.Message);

            }
        }
    }
}


